package com.glut.controller;

import com.github.pagehelper.Page;
import com.glut.service.LevKldService;
import com.glut.service.SdfsQuestionsService;
import com.glut.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Author:沈林玉
 * @date:2020/2/14 10:19
 * @Description:
 */
@Controller
@RequestMapping("/question")
public class SdfsQuestionsController {

    @Autowired
    SdfsQuestionsService sdfsQuestionsService;
    @Autowired
    LevKldService levKldService;

    Logger logger = Logger.getLogger(SdfsQuestionsController.class);


    /**
     * 1.查询总试题
     *
     * @param sdfsQuestions
     * @return
     */
    @RequestMapping(value = "/selectAllSdfsQuestions")
    public List<SdfsQuestions> selectAllSdfsQuestions(SdfsQuestions sdfsQuestions) {
        List<SdfsQuestions> sdfsQuestionsList = sdfsQuestionsService.selectAllSdfsQuestions("");
        return sdfsQuestionsList;
    }

    /**
     * 2.查询单选题
     *
     * @param sQuestionDetail
     * @return
     */
    @RequestMapping("/selectAllSQuestions/{pageNum}")
    public String selectAllSQuestions(SQuestionDetail sQuestionDetail,
                                      SdfsQuestions sdfsQuestions,
                                      @PathVariable Integer pageNum,
                                      ModelMap modelMap,
                                      HttpSession session) {
        sQuestionDetail.setSdfsQuestions(sdfsQuestions);
        if (pageNum == null || pageNum < 1) {
            // 保留查询条件
            session.setAttribute("sQCondition", sQuestionDetail);
            pageNum = 1;
        } else {
            // 点击分页
            sQuestionDetail = (SQuestionDetail) session.getAttribute("sQCondition");
        }
        Page<SQuestionDetail> sQuestionDetailList = (Page<SQuestionDetail>)sdfsQuestionsService.selectAllSQuestions(sQuestionDetail, pageNum, 10);

        modelMap.addAttribute("sQuestionList", sQuestionDetailList);
        return "question/single_select";
    }

    /**
     * 2.通过ID查询sdfs题
     *
     * @param sQuestionDetail
     * @return
     */
    @RequestMapping("/selectSdfsQuestionsByID/{questionType}")
    public String selectSdfsQuestionsByID(@PathVariable Integer questionType,
                                       SQuestionDetail sQuestionDetail,
                                       DQuestionDetail dQuestionDetail,
                                       FQuestionDetail fQuestionDetail,
                                       ShQuestionDetail shQuestionDetail,
                                       HttpSession session) {
        if (questionType==1){
            SQuestionDetail sQByID = sdfsQuestionsService.selectSQuestionsByID(sQuestionDetail);
            List<SecKld> secKldList = new ArrayList<SecKld>();
            String secKldID = sQByID.getSdfsQuestions().getSecKld();

            // 进行分割查询二级知识点的名字
            String[] s = secKldID.split(",");

            for (int i = 0; i < s.length; i++) {
                String secKldCtgr = levKldService.selectSecKldIDByID(new SecKld(s[i], null, null));
                SecKld secKld = new SecKld();
                secKld.setSecKldID(s[i]);
                secKld.setSecKldCtgr(secKldCtgr);
                secKldList.add(secKld);
            }
            session.setAttribute("sQSecKldList",secKldList);
            session.setAttribute("sQByID",sQByID);
        }else if (questionType==2){
            DQuestionDetail sQByID = sdfsQuestionsService.selectDQuestionsByID(dQuestionDetail);
            List<SecKld> secKldList = new ArrayList<SecKld>();
            String secKldID = sQByID.getSdfsQuestions().getSecKld();

            // 进行分割查询二级知识点的名字
            String[] s = secKldID.split(",");

            for (int i = 0; i < s.length; i++) {
                String secKldCtgr = levKldService.selectSecKldIDByID(new SecKld(s[i], null, null));
                SecKld secKld = new SecKld();
                secKld.setSecKldID(s[i]);
                secKld.setSecKldCtgr(secKldCtgr);
                secKldList.add(secKld);
            }
            session.setAttribute("sQSecKldList",secKldList);
            session.setAttribute("sQByID",sQByID);
        }else if (questionType==3) {
            FQuestionDetail sQByID = sdfsQuestionsService.selectFQuestionsByID(fQuestionDetail);
            List<SecKld> secKldList = new ArrayList<SecKld>();
            String secKldID = sQByID.getSdfsQuestions().getSecKld();

            // 进行分割查询二级知识点的名字
            String[] s = secKldID.split(",");

            for (int i = 0; i < s.length; i++) {
                String secKldCtgr = levKldService.selectSecKldIDByID(new SecKld(s[i], null, null));
                SecKld secKld = new SecKld();
                secKld.setSecKldID(s[i]);
                secKld.setSecKldCtgr(secKldCtgr);
                secKldList.add(secKld);
            }
            session.setAttribute("sQSecKldList",secKldList);
            session.setAttribute("sQByID", sQByID);
        }else  if (questionType==4 || questionType==5){
            ShQuestionDetail sQByID = sdfsQuestionsService.selectShQuestionsByID(shQuestionDetail);
            List<SecKld> secKldList = new ArrayList<SecKld>();
            String secKldID = sQByID.getSdfsQuestions().getSecKld();

            // 进行分割查询二级知识点的名字
            String[] s = secKldID.split(",");

            for (int i = 0; i < s.length; i++) {
                String secKldCtgr = levKldService.selectSecKldIDByID(new SecKld(s[i], null, null));
                SecKld secKld = new SecKld();
                secKld.setSecKldID(s[i]);
                secKld.setSecKldCtgr(secKldCtgr);
                secKldList.add(secKld);
            }
            session.setAttribute("sQSecKldList",secKldList);
            session.setAttribute("sQByID",sQByID);
        }
        return "redirect:/views/question/updateForm.jsp";
    }

    /**
     * 3.查询判断题
     * @param dQuestionDetail
     * @return
     */
    @RequestMapping( "/selectAllDQuestions/{pageNum}")
    public String selectAllDQuestions(DQuestionDetail dQuestionDetail,
                                      SdfsQuestions sdfsQuestions,
                                      @PathVariable Integer pageNum,
                                      ModelMap modelMap,
                                      HttpSession session) {
        dQuestionDetail.setSdfsQuestions(sdfsQuestions);
        if (pageNum == null || pageNum < 1) {
            // 保留查询条件
            session.setAttribute("dQCondition", dQuestionDetail);
            pageNum = 1;
        } else {
            // 点击分页
            dQuestionDetail = (DQuestionDetail) session.getAttribute("dQCondition");
        }
        Page<DQuestionDetail> dQuestionDetailList = (Page<DQuestionDetail>)sdfsQuestionsService.selectAllDQuestions(dQuestionDetail, pageNum, 10);

        modelMap.addAttribute("dQuestionList", dQuestionDetailList);
        return "question/judge_select";
    }



    /**
     * 4.查询填空题
     * @param fQuestionDetail
     * @return
     */

    @RequestMapping( "/selectAllFQuestions/{pageNum}")
    public String selectAllFQuestions(FQuestionDetail fQuestionDetail,
                                      SdfsQuestions sdfsQuestions,
                                      @PathVariable Integer pageNum,
                                      ModelMap modelMap,
                                      HttpSession session){
        fQuestionDetail.setSdfsQuestions(sdfsQuestions);
        if(pageNum==null||pageNum< 1){
        // 保留查询条件
        session.setAttribute("fQCondition",fQuestionDetail);
        pageNum=1;
        }else{
        // 点击分页
        fQuestionDetail=(FQuestionDetail)session.getAttribute("fQCondition");
        }
        Page<FQuestionDetail> fQuestionDetailList = (Page<FQuestionDetail>)sdfsQuestionsService.selectAllFQuestions(fQuestionDetail,pageNum,10);

        modelMap.addAttribute("fQuestionList",fQuestionDetailList);
        return"question/fill_select";
        }

    /**
     * 5.查询简答题
     * @param shQuestionDetail
     * @return
     */
    @RequestMapping( "/selectAllShQuestions/{pageNum}")
    public String selectAllShQuestions(ShQuestionDetail shQuestionDetail,
                                       SdfsQuestions sdfsQuestions,
                                       @PathVariable Integer pageNum,
                                       ModelMap modelMap,
                                       HttpSession session){
        shQuestionDetail.setSdfsQuestions(sdfsQuestions);
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            session.setAttribute("shQCondition",shQuestionDetail);
            pageNum=1;
        }else{
            // 点击分页
            shQuestionDetail=(ShQuestionDetail)session.getAttribute("shQCondition");
        }
        Page<ShQuestionDetail> shQuestionDetailList = (Page<ShQuestionDetail>)sdfsQuestionsService.selectAllShQuestions(shQuestionDetail,pageNum,10);

        modelMap.addAttribute("shQuestionList",shQuestionDetailList);
        return"question/shortAnswer_select";
    }


    /**
     * 1.新增单选题
     * @param questionType
     * @param sdfsQuestions
     * @param sQuestionDetail
     * @param
     * @return
     */
    @RequestMapping("/insertSQuestion/{questionType}")
    public String insertSQuestionDetail(@PathVariable Integer questionType,
                                        SdfsQuestions sdfsQuestions,
                                        SQuestionDetail sQuestionDetail,
                                        HttpSession session){
        // 设置试题类型
        sdfsQuestions.setQuestionType(questionType);

        // 设置总试题属性到详细表里以便后期插入
        sQuestionDetail.setSdfsQuestions(sdfsQuestions);

        // 进行数据库插入操作
        int insertSQuestion = sdfsQuestionsService.insertSQuestionDetail(sQuestionDetail);

        // 存入session中以便页面进行判断
        session.setAttribute("insertSQuestion",insertSQuestion);

        // 重定向回到页面
        return "redirect:/views/question/single_input.jsp";
    }


    /**
     * 2.新增判断题
     * @param questionType
     * @param sdfsQuestions
     * @param dQuestionDetail
     * @param session
     * @return
     */
    @RequestMapping("/insertDQuestion/{questionType}")
    public String insertDQuestionDetail(@PathVariable Integer questionType,
                                        SdfsQuestions sdfsQuestions,
                                        DQuestionDetail dQuestionDetail,
                                        HttpSession session){
        // 设置试题类型
        sdfsQuestions.setQuestionType(questionType);

        // 设置总试题属性到详细表里以便后期插入
        dQuestionDetail.setSdfsQuestions(sdfsQuestions);

        // 进行数据库插入操作
        int insertDQuestion = sdfsQuestionsService.insertDQuestionDetail(dQuestionDetail);

        //request.setAttribute("insertDQuestion",insertDQuestion);
        // 存入session中以便页面进行查询判断
        session.setAttribute("insertDQuestion",insertDQuestion);

        // 重定向回到页面
        return "redirect:/views/question/judge_input.jsp";
    }

    /**
     * 3.新增填空题
     * @param questionType
     * @param sdfsQuestions
     * @param fQuestionDetail
     * @param session
     * @return
     */
    @RequestMapping("/insertFQuestion/{questionType}")
    public String insertFQuestionDetail(@PathVariable Integer questionType,
                                        SdfsQuestions sdfsQuestions,
                                        FQuestionDetail fQuestionDetail,
                                        HttpSession session){
        // 设置试题类型
        sdfsQuestions.setQuestionType(questionType);

        // 设置总试题属性到详细表里以便后期插入
        fQuestionDetail.setSdfsQuestions(sdfsQuestions);

        // 进行数据库插入操作
        int insertFQuestion = sdfsQuestionsService.insertFQuestionDetail(fQuestionDetail);

        // 存入session中以便页面进行查询判断
        session.setAttribute("insertFQuestion",insertFQuestion);

        // 重定向回到页面
        return "redirect:/views/question/fill_input.jsp";
    }

    /**
     * 4.新增简答题
     * @param questionType
     * @param sdfsQuestions
     * @param shQuestionDetail
     * @param session
     * @return
     */
    @RequestMapping("/insertShQuestion/{questionType}")
    public String insertShQuestionDetail(MultipartFile file,
                                        @PathVariable Integer questionType,
                                        SdfsQuestions sdfsQuestions,
                                        ShQuestionDetail shQuestionDetail,
                                        HttpSession session){
        try {
        /**
         * 上传图片
         */
        //图片上传成功后，将图片的地址写到数据库
        String filePath = "D:\\a蓝桥\\java web中级项目\\ssts\\ssts\\src\\main\\webapp\\images\\upload";//保存图片的路径
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isNotBlank(originalFilename)){
            //新的文件名字
            String newFileName = UUID.randomUUID()+originalFilename;
            //封装上传文件位置的全路径
            File targetFile = new File(filePath,newFileName);
            //把本地文件上传到封装上传文件位置的全路径
            file.transferTo(targetFile);
            shQuestionDetail.setShQuestionImage(newFileName);
        }else {
            shQuestionDetail.setShQuestionImage(originalFilename);
        }
        } catch (IOException e) {
            logger.error("获取图片失败："+e);
            session.setAttribute("insertShQuestion",0);
            return "redirect:/views/question/shortAnswer_input.jsp";
        }
        // 设置试题类型
        sdfsQuestions.setQuestionType(questionType);


        // 设置总试题属性到详细表里以便后期插入
        shQuestionDetail.setSdfsQuestions(sdfsQuestions);

        // 进行数据库插入操作
        int insertShQuestionDetail = sdfsQuestionsService.insertShQuestionDetail(shQuestionDetail);

        // 存入session中以便页面进行查询判断
        session.setAttribute("insertShQuestion",insertShQuestionDetail);
        if (questionType==4){
            // 重定向回到页面
            return "redirect:/views/question/shortAnswer_input.jsp";
        }else {
            // 重定向回到页面
            return "redirect:/views/question/sythesis_input.jsp";
        }
    }

    /**
     * 5.批量导入试题
     * @param file
     * @param questionType
     * @param session
     * @return
     */
    @RequestMapping(value = "importQuestion/{questionType}" ,method = RequestMethod.POST)
    public String importQuestion(MultipartFile file,
                                 @PathVariable Integer questionType,
                                 HttpSession session){
        Integer importQ = null;
        if (!file.isEmpty()) {

            InputStream is = null;
            try {
                // 获得文件输入流
                is = file.getInputStream();

                if(questionType==1){
                     importQ = importSQ(file, is);
                }else if(questionType==2){
                     importQ = importDQ(file, is);
                }else if(questionType==3){
                     importQ = importFQ(file, is);
                }
            }catch (IOException e) {
                logger.debug("获得文件流错误!" + e);
                session.setAttribute("importQ",0);
                return  "redirect:/views/question/batch_import.jsp";
            }catch (Exception e) {
                logger.debug("批量导入试题失败!" + e);
                session.setAttribute("importQ",0);
                return  "redirect:/views/question/batch_import.jsp";
            }
        }
        session.setAttribute("importQ",importQ);

        return  "redirect:/views/question/batch_import.jsp";
    }

    /**
     * 5.1批量导入单选
     * @param file
     * @param is
     * @return
     */
    public Integer importSQ(MultipartFile file,InputStream is){
        Integer flag = null;
        try {

        // 创建一个集合
        List<Object> list = new ArrayList<Object>();
        // 定义一个Workbook工作本
        Workbook workbook = null;

        // 获得上传文件名
        String originalFilename = file.getOriginalFilename();
        // 判断接收时的文件格式
        if (originalFilename.endsWith(".xls")) {

                workbook = new HSSFWorkbook(is);

            } else if (originalFilename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(is);
            }

            // 获得sheet得到第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获得工作表中的行数
            int numberOfRows = sheet.getPhysicalNumberOfRows();

            // 跳过第一行
            for (int rowIndex = 1; rowIndex < numberOfRows; rowIndex++) {
                // 获得每一行
                Row row = sheet.getRow(rowIndex);

                // 获得每一个单元格
                Cell cell = null;

                // 获得表格对应值
                 cell = row.getCell(0);
                String question = cell.getStringCellValue();

                cell = row.getCell(1);
                String optionA = cell.getStringCellValue();

                cell = row.getCell(2);
                String optionB = cell.getStringCellValue();

                cell = row.getCell(3);
                String optionC = cell.getStringCellValue();

                cell = row.getCell(4);
                String optionD = cell.getStringCellValue();

                cell = row.getCell(5);
                String optionE = cell.getStringCellValue();

                cell = row.getCell(6);
                String sCorrect = cell.getStringCellValue();

                cell = row.getCell(7);
                String di = cell.getStringCellValue();

                cell = row.getCell(8);
                String levKld = cell.getStringCellValue();
                // 查询一级知识点的ID
                String levKldID = levKldService.selectLevKldIDByName(new LevKld(null, levKld.replaceAll(" ","")));

                cell = row.getCell(9);
                String secKld = cell.getStringCellValue();
                System.out.println(secKld);
                // 进行分割查询二级知识点的ID
                String[] s = secKld.split(",");
                StringBuffer secKldAll = new StringBuffer();
                for (int i = 0; i < s.length; i++) {
                    System.out.println(s[i]);
                    String secKldID = levKldService.selectSecKldIDByName(new SecKld(null, s[i], levKldID));
                    secKldAll.append(secKldID);
                    if (i!= (s.length)-1) {
                        secKldAll.append(",");
                    }
                }

                // 设置属性值
                // 创建自定义试题对象
                SQuestionDetail sQuestionDetail = new SQuestionDetail();
                // 创建主试题对象
                SdfsQuestions sdfsQuestions = new SdfsQuestions();
                sdfsQuestions.setQuestion(question);
                sQuestionDetail.setOptionA(optionA);
                sQuestionDetail.setOptionB(optionB);
                sQuestionDetail.setOptionC(optionC);
                sQuestionDetail.setOptionD(optionD);
                sQuestionDetail.setOptionE(optionE);
                sQuestionDetail.setsCorrect(sCorrect);
                if (di.equals("简单")){
                    sdfsQuestions.setDifficulty(0.25);
                }else if (di.equals("一般")){
                    sdfsQuestions.setDifficulty(0.5);
                }else if (di.equals("困难")){
                    sdfsQuestions.setDifficulty(0.75);
                }
                sdfsQuestions.setSecKld(secKldAll.toString());
                sdfsQuestions.setQuestionType(1);
                sQuestionDetail.setSdfsQuestions(sdfsQuestions);
                logger.debug(sQuestionDetail);
                System.out.println(sQuestionDetail);
                list.add(sQuestionDetail);
            }

                int i = sdfsQuestionsService.saveBatchImportQuestion(list,1);
                if (i!=0){
                    flag = i;
                }else{
                    flag = 0;
                }
        }catch (IOException e) {
            e.printStackTrace();
        }
     return flag;
    }

    /**
     * 5.2批量导入判断
     * @param file
     * @param is
     * @return
     */
    public Integer importDQ(MultipartFile file,InputStream is){
        Integer flag = null;
        try {

            // 创建一个集合
            List<Object> list = new ArrayList<Object>();
            // 定义一个Workbook工作本
            Workbook workbook = null;

            // 获得上传文件名
            String originalFilename = file.getOriginalFilename();
            // 判断接收时的文件格式
            if (originalFilename.endsWith(".xls")) {

                workbook = new HSSFWorkbook(is);

            } else if (originalFilename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(is);
            }

            // 获得sheet得到第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获得工作表中的行数
            int numberOfRows = sheet.getPhysicalNumberOfRows();

            // 跳过第一行
            for (int rowIndex = 1; rowIndex < numberOfRows; rowIndex++) {
                // 获得每一行
                Row row = sheet.getRow(rowIndex);

                // 获得每一个单元格
                Cell cell = null;

                // 获得表格对应值
                cell = row.getCell(0);
                String question = cell.getStringCellValue();

                cell = row.getCell(1);
                String dCorrect = cell.getStringCellValue();

                cell = row.getCell(2);
                String di = cell.getStringCellValue();

                cell = row.getCell(3);
                String levKld = cell.getStringCellValue();
                // 查询一级知识点的ID
                String levKldID = levKldService.selectLevKldIDByName(new LevKld(null, levKld.replaceAll(" ","")));
                System.out.println(levKldID);
                cell = row.getCell(4);
                String secKld = cell.getStringCellValue();
                System.out.println(secKld);
                // 进行分割查询二级知识点的ID
                String[] s = secKld.split(",");
                StringBuffer secKldAll = new StringBuffer();
                for (int i = 0; i < s.length; i++) {
                    System.out.println(s[i]);
                    String secKldID = levKldService.selectSecKldIDByName(new SecKld(null, s[i], levKldID));
                    secKldAll.append(secKldID);
                    if (i!= (s.length)-1) {
                        secKldAll.append(",");
                    }
                }

                // 设置属性值
                // 创建自定义试题对象
                DQuestionDetail dQuestionDetail = new DQuestionDetail();
                // 创建主试题对象
                SdfsQuestions sdfsQuestions = new SdfsQuestions();
                sdfsQuestions.setQuestion(question);
                dQuestionDetail.setdCorrect(dCorrect);
                if (di.equals("简单")){
                    sdfsQuestions.setDifficulty(0.25);
                }else if (di.equals("一般")){
                    sdfsQuestions.setDifficulty(0.5);
                }else if (di.equals("困难")){
                    sdfsQuestions.setDifficulty(0.75);
                }
                sdfsQuestions.setSecKld(secKldAll.toString());
                sdfsQuestions.setQuestionType(2);
                dQuestionDetail.setSdfsQuestions(sdfsQuestions);
                logger.debug(dQuestionDetail);
                System.out.println(dQuestionDetail);
                list.add(dQuestionDetail);
            }

            int i = sdfsQuestionsService.saveBatchImportQuestion(list,2);
            if (i!=0){
                flag = i;
            }else{
                flag = 0;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 5.3批量导入填空
     * @param file
     * @param is
     * @return
     */
    public Integer importFQ(MultipartFile file,InputStream is){
        Integer flag = null;
        try {

            // 创建一个集合
            List<Object> list = new ArrayList<Object>();
            // 定义一个Workbook工作本
            Workbook workbook = null;

            // 获得上传文件名
            String originalFilename = file.getOriginalFilename();
            // 判断接收时的文件格式
            if (originalFilename.endsWith(".xls")) {

                workbook = new HSSFWorkbook(is);

            } else if (originalFilename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(is);
            }

            // 获得sheet得到第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获得工作表中的行数
            int numberOfRows = sheet.getPhysicalNumberOfRows();

            // 跳过第一行
            for (int rowIndex = 1; rowIndex < numberOfRows; rowIndex++) {
                // 获得每一行
                Row row = sheet.getRow(rowIndex);

                // 获得每一个单元格
                Cell cell = null;

                // 获得表格对应值
                cell = row.getCell(0);
                String question = cell.getStringCellValue();

                cell = row.getCell(1);
                String fCorrect = cell.getStringCellValue();

                cell = row.getCell(2);
                String di = cell.getStringCellValue();

                cell = row.getCell(3);
                String levKld = cell.getStringCellValue();
                // 查询一级知识点的ID
                String levKldID = levKldService.selectLevKldIDByName(new LevKld(null, levKld.replaceAll(" ","")));

                cell = row.getCell(4);
                String secKld = cell.getStringCellValue();
                System.out.println(secKld);
                // 进行分割查询二级知识点的ID
                String[] s = secKld.split(",");
                StringBuffer secKldAll = new StringBuffer();
                for (int i = 0; i < s.length; i++) {
                    System.out.println(s[i]);
                    String secKldID = levKldService.selectSecKldIDByName(new SecKld(null, s[i], levKldID));
                    secKldAll.append(secKldID);
                    if (i!= (s.length)-1) {
                        secKldAll.append(",");
                    }
                }

                // 设置属性值
                // 创建自定义试题对象
                FQuestionDetail fQuestionDetail = new FQuestionDetail();
                // 创建主试题对象
                SdfsQuestions sdfsQuestions = new SdfsQuestions();
                sdfsQuestions.setQuestion(question);
                fQuestionDetail.setfCorrect(fCorrect);
                if (di.equals("简单")){
                    sdfsQuestions.setDifficulty(0.25);
                }else if (di.equals("一般")){
                    sdfsQuestions.setDifficulty(0.5);
                }else if (di.equals("困难")){
                    sdfsQuestions.setDifficulty(0.75);
                }
                sdfsQuestions.setSecKld(secKldAll.toString());
                sdfsQuestions.setQuestionType(3);
                fQuestionDetail.setSdfsQuestions(sdfsQuestions);
                logger.debug(fQuestionDetail);
                System.out.println(fQuestionDetail);
                list.add(fQuestionDetail);
            }

            int i = sdfsQuestionsService.saveBatchImportQuestion(list,3);
            if (i!=0){
                flag = i;
            }else{
                flag = 0;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 5.4批量导入简答
     * @param file
     * @param is
     */
    public void importShQ(MultipartFile file,InputStream is){
        try {
            // 创建自定义试题对象
            SQuestionDetail sQuestionDetail = new SQuestionDetail();

            // 创建一个集合
            ArrayList<Object> list = new ArrayList<Object>();
            // 定义一个Workbook工作本
            Workbook workbook = null;

            // 获得上传文件名
            String originalFilename = file.getOriginalFilename();
            // 判断接收时的文件格式
            if (originalFilename.endsWith(".xls")) {

                workbook = new HSSFWorkbook(is);

            } else if (originalFilename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(is);
            }

            // 获得sheet得到第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获得工作表中的行数
            int numberOfRows = sheet.getPhysicalNumberOfRows();

            // 跳过第一行
            for (int rowIndex = 1; rowIndex < numberOfRows; rowIndex++) {
                // 获得每一行
                Row row = sheet.getRow(rowIndex);

                // 获得每一个单元格
                Cell cell = null;

                // 设置属性值
                sQuestionDetail.getSdfsQuestions().setQuestion(row.getCell(0).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(1).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(2).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(3).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(4).getStringCellValue());
                // 跳过空行
                if (row.getCell(5) == null || StringUtils.isBlank(row.getCell(5).getStringCellValue())) {
                    continue;
                } else {
                    sQuestionDetail.setOptionE(row.getCell(5).getStringCellValue());
                }
                sQuestionDetail.setsCorrect(row.getCell(6).getStringCellValue());
                String difficulty = row.getCell(7).getStringCellValue();
                sQuestionDetail.getSdfsQuestions().setDifficulty(Double.parseDouble(difficulty));

                String levKld = row.getCell(8).getStringCellValue();
                // 查询一级知识点的ID
                String levKldID = levKldService.selectLevKldIDByName(new LevKld(null, levKld.replaceAll(" ","")));

                String secKld = row.getCell(9).getStringCellValue();
                // 进行分割查询二级知识点的ID
                String[] split = secKld.split(",");
                StringBuffer secKldAll = null;
                for (int i = 0; i < split.length; i++) {
                    String secKldID = levKldService.selectSecKldIDByName(new SecKld(null, split[i], levKldID));
                    secKldAll.append(secKldID);
                    secKldAll.append(",");
                    if (i >= split.length - 1) {
                        secKldAll.delete(secKldAll.length() - 1, secKldAll.length() - 1);
                    }
                }

                sQuestionDetail.getSdfsQuestions().setSecKld(secKldAll.toString());
                sQuestionDetail.getSdfsQuestions().setQuestionType(4);
                logger.debug(sQuestionDetail);
                list.add(sQuestionDetail);
            }

            if (list.size() > 0) {
                int i = sdfsQuestionsService.saveBatchImportQuestion(list,4);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 5.5批量导入综合
     * @param file
     * @param is
     */
    public void importSynQ(MultipartFile file,InputStream is){
        try {
            // 创建自定义试题对象
            SQuestionDetail sQuestionDetail = new SQuestionDetail();

            // 创建一个集合
            ArrayList<Object> list = new ArrayList<Object>();
            // 定义一个Workbook工作本
            Workbook workbook = null;

            // 获得上传文件名
            String originalFilename = file.getOriginalFilename();
            // 判断接收时的文件格式
            if (originalFilename.endsWith(".xls")) {

                workbook = new HSSFWorkbook(is);

            } else if (originalFilename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(is);
            }

            // 获得sheet得到第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 获得工作表中的行数
            int numberOfRows = sheet.getPhysicalNumberOfRows();

            // 跳过第一行
            for (int rowIndex = 1; rowIndex < numberOfRows; rowIndex++) {
                // 获得每一行
                Row row = sheet.getRow(rowIndex);

                // 获得每一个单元格
                Cell cell = null;

                // 设置属性值
                sQuestionDetail.getSdfsQuestions().setQuestion(row.getCell(0).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(1).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(2).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(3).getStringCellValue());
                sQuestionDetail.setOptionA(row.getCell(4).getStringCellValue());
                // 跳过空行
                if (row.getCell(5) == null || StringUtils.isBlank(row.getCell(5).getStringCellValue())) {
                    continue;
                } else {
                    sQuestionDetail.setOptionE(row.getCell(5).getStringCellValue());
                }
                sQuestionDetail.setsCorrect(row.getCell(6).getStringCellValue());
                String difficulty = row.getCell(7).getStringCellValue();
                sQuestionDetail.getSdfsQuestions().setDifficulty(Double.parseDouble(difficulty));

                String levKld = row.getCell(8).getStringCellValue();
                // 查询一级知识点的ID
                String levKldID = levKldService.selectLevKldIDByName(new LevKld(null, levKld.replaceAll(" ","")));

                String secKld = row.getCell(9).getStringCellValue();
                // 进行分割查询二级知识点的ID
                String[] split = secKld.split(",");
                StringBuffer secKldAll = null;
                for (int i = 0; i < split.length; i++) {
                    String secKldID = levKldService.selectSecKldIDByName(new SecKld(null, split[i], levKldID));
                    secKldAll.append(secKldID);
                    secKldAll.append(",");
                    if (i >= split.length - 1) {
                        secKldAll.delete(secKldAll.length() - 1, secKldAll.length() - 1);
                    }
                }

                sQuestionDetail.getSdfsQuestions().setSecKld(secKldAll.toString());
                sQuestionDetail.getSdfsQuestions().setQuestionType(5);
                logger.debug(sQuestionDetail);
                list.add(sQuestionDetail);
            }

            if (list.size() > 0) {
                int i = sdfsQuestionsService.saveBatchImportQuestion(list,5);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 6.更新单选题
     * @param sdfsQuestions
     * @param sQuestionDetail
     * @return
     */
    @RequestMapping("/updateSQ")
    @ResponseBody
    public Map<String,Integer> updateSQuestions(SdfsQuestions sdfsQuestions,
                                   SQuestionDetail sQuestionDetail){
        sQuestionDetail.setSdfsQuestions(sdfsQuestions);
        // 存入map中
        Map<String, Object> map = new HashMap<>();
        map.put("t_questionID", sQuestionDetail.getsQuestionID());
        map.put("t_question", sQuestionDetail.getSdfsQuestions().getQuestion());
        map.put("t_difficulty", sQuestionDetail.getSdfsQuestions().getDifficulty());
        map.put("t_secKld", sQuestionDetail.getSdfsQuestions().getSecKld());
        map.put("t_option_a", sQuestionDetail.getOptionA());
        map.put("t_option_b", sQuestionDetail.getOptionB());
        map.put("t_option_c", sQuestionDetail.getOptionC());
        map.put("t_option_d", sQuestionDetail.getOptionD());
        map.put("t_option_e", sQuestionDetail.getOptionE());
        map.put("t_s_correct", sQuestionDetail.getsCorrect());
        // 进行更新
        sdfsQuestionsService.updateSQuestions(map);
        int updateSQ = (int) map.get("t_result");

        // 将更新结果存入map中返回页面进行判断
        Map<String,Integer> map1 = new HashMap<String, Integer>();
        map1.put("updateSQ",updateSQ);
        return map1;
    }

    /**
     * 7.更新dfsh题
     * @param sdfsQuestions
     * @param
     * @return
     */
    @RequestMapping("/updateDfshQ")
    @ResponseBody
    public Map<String,Integer> updateSQuestions(SdfsQuestions sdfsQuestions,
                                                DQuestionDetail dQuestionDetail,
                                                FQuestionDetail fQuestionDetail,
                                                ShQuestionDetail shQuestionDetail){
        Map<String, Object> map = new HashMap<>();
        // 进行类型判断并存入map中  ,@PathVariable Integer questionType,
        //
        if (sdfsQuestions.getQuestionType()==2){
            dQuestionDetail.setSdfsQuestions(sdfsQuestions);
            map.put("t_questionID", dQuestionDetail.getdQuestionID());
            map.put("t_question", dQuestionDetail.getSdfsQuestions().getQuestion());
            map.put("t_difficulty", dQuestionDetail.getSdfsQuestions().getDifficulty());
            map.put("t_secKld", dQuestionDetail.getSdfsQuestions().getSecKld());
            map.put("t_d_correct", dQuestionDetail.getdCorrect());
            map.put("t_f_correct", fQuestionDetail.getfCorrect());
            map.put("t_sh_correct", shQuestionDetail.getShCorrect());
            map.put("t_syn_correct", shQuestionDetail.getShCorrect());
            map.put("t_question_type", 2);
        }else if (sdfsQuestions.getQuestionType()==3){
            fQuestionDetail.setSdfsQuestions(sdfsQuestions);
            map.put("t_questionID", fQuestionDetail.getfQuestionID());
            map.put("t_question", fQuestionDetail.getSdfsQuestions().getQuestion());
            map.put("t_difficulty", fQuestionDetail.getSdfsQuestions().getDifficulty());
            map.put("t_secKld", fQuestionDetail.getSdfsQuestions().getSecKld());
            map.put("t_d_correct", dQuestionDetail.getdCorrect());
            map.put("t_f_correct", fQuestionDetail.getfCorrect());
            map.put("t_sh_correct", shQuestionDetail.getShCorrect());
            map.put("t_syn_correct", shQuestionDetail.getShCorrect());
            map.put("t_question_type", 3);
        }else if (sdfsQuestions.getQuestionType()==4 || sdfsQuestions.getQuestionType()==5){
            shQuestionDetail.setSdfsQuestions(sdfsQuestions);
            map.put("t_questionID", shQuestionDetail.getShQuestionID());
            map.put("t_question", shQuestionDetail.getSdfsQuestions().getQuestion());
            map.put("t_difficulty", shQuestionDetail.getSdfsQuestions().getDifficulty());
            map.put("t_secKld", shQuestionDetail.getSdfsQuestions().getSecKld());
            map.put("t_d_correct", dQuestionDetail.getdCorrect());
            map.put("t_f_correct", fQuestionDetail.getfCorrect());
            map.put("t_sh_correct", shQuestionDetail.getShCorrect());
            map.put("t_syn_correct", shQuestionDetail.getShCorrect());
            map.put("t_question_type", 4);
        }
        // 进行更新
        sdfsQuestionsService.updatedfshQuestions(map);
        int updateDfshQ = (int) map.get("t_result");

        // 将更新结果存入map中返回页面进行判断
        Map<String,Integer> map1 = new HashMap<String, Integer>();
        map1.put("updateDfshQ",updateDfshQ);
        return map1;
    }

    /**
     * 8.删除sdfs题
     * @param sdfsQuestions
     * @param
     * @return
     */
    @RequestMapping("/deleteSdfsQ/{questionType}")
    @ResponseBody
    public Map<String,Integer> deleteSdfsQuestions(@PathVariable Integer questionType,
                                                    SdfsQuestions sdfsQuestions){
        Map<String, Object> map = new HashMap<>();
        map.put("t_questionID", sdfsQuestions.getSdfsQuestionID());
        map.put("t_question_type", questionType);
        // 进行删除
        sdfsQuestionsService.deleteSdfsQuestions(map);
        int deleteSdfsQ = (int) map.get("t_result");

        // 将删除结果存入map中返回页面进行判断
        Map<String,Integer> map1 = new HashMap<String, Integer>();
        map1.put("deleteSdfsQ",deleteSdfsQ);
        return map1;
    }
}


